package com.itcast.discover.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.common.utils.BeanHelper;
import com.itcast.discover.mapper.DiscoverCMapper;
import com.itcast.discover.mapper.DiscoverMapper;
import com.pojo.discover.dto.DiscoverDto;
import com.pojo.discover.pojo.Discover;
import com.pojo.discover.pojo.DiscoverC;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.lang.reflect.Array;
import java.util.*;


/*
* 发现
* */
@Service
@Transactional
@Slf4j
public class DiscoverService {

    @Autowired(required = false)
    private DiscoverMapper discoverMapper;
    @Autowired(required = false)
    private DiscoverCService discoverCService;

    /*
    * 分页查询
    * */
    public List<Discover> findDiscoverByPage(Integer page, Integer rows, Discover discover) {

        IPage<Discover> ipage = new Page(page,rows);
        QueryWrapper<Discover> queryWrapper = Wrappers.query();
        if(discover!=null){
            if(StringUtils.isNotEmpty(discover.getTitle())){
                queryWrapper.like("title",discover.getTitle());
            }
            if(discover.getCreateTime()!=null){
                queryWrapper.eq("create_time",discover.getCreateTime());
            }
            if(discover.getSId()!=null){
                queryWrapper.eq("s_id",discover.getSId());
            }
            if (discover.getUid()!=null){
                queryWrapper.eq("uid",discover.getUid());
            }
            if(discover.getType()!=null){
                queryWrapper.eq("type",discover.getType());
            }
        }


       
        IPage<Discover> discoverIPage = null;
        try {
            discoverIPage = discoverMapper.selectPage(ipage, queryWrapper);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        List<Discover> discoverList = discoverIPage.getRecords();
        return discoverList;

    }

    /*
    * 根据id查找discover并进行数据封装
    * */
    public DiscoverDto findDiscoverById(Integer id) {

        Discover discover = discoverMapper.selectById(id);
        DiscoverDto discoverDto = BeanHelper.copyProperties(discover, DiscoverDto.class);

//        配件产品的封装
//        String[] split = discover.getPids().split(",");
//        List<String> pids = Arrays.asList(split);
//        discoverDto.setPeijianList( discoverCMapper.selectBatchIds(pids));

        List<DiscoverC> discoverCAll = discoverCService.findDiscoverCAll();
        discoverDto.setDiscoverClist(discoverCAll);

        return discoverDto;

    }

    /*
    * 根据分类id判断是否存在对应的发现
    * */
    public Boolean findDiscoverBySid(Integer cid) {
        Discover discover = new Discover();
        discover.setSId(cid);
        QueryWrapper<Discover> query = Wrappers.query(discover);
        Integer count = discoverMapper.selectCount(query);
        if(count>0){
            return true;
        }else{
            return false;
        }
    }
}
